/*
 * Copyright (C) 2004, Velocityme Partnership
 * ProjectActions.java
 *
 * Created on 06 October 2004, 08:17
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.ProjectValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import javax.swing.tree.TreeModel;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class ProjectActions extends TaskActions {
    
    /** Creates a new instance of ProjectActions */
    public ProjectActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ProjectPK projectPK = new ProjectPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("interestedContactableValues", cs.getTaskInterestedContactableValues(key, taskPK));
        session.setAttribute("responsibleContactableValues", cs.getTaskResponsibleContactableValues(key, taskPK));
        session.setAttribute("projectValue", cs.getProjectValue(key, projectPK));
        session.setAttribute("projectHistory", cs.getHistoryValue(key, nodePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ProjectPK projectPK = new ProjectPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        session.setAttribute("personPairs", NodeUtil.getAllPersonPairs(key, cs));
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        session.setAttribute("taskTypePairs", NodeUtil.getAllTaskTypePairs(key, cs));
        ProjectValueActionForm projectValueActionForm = new ProjectValueActionForm();
        projectValueActionForm.setOwnerId(key.getPersonPK().getPersonId());
        projectValueActionForm.setPercentageComplete(new Byte((byte)0));
        projectValueActionForm.setIsInterested(Boolean.TRUE);
        projectValueActionForm.setDurationTotalMins(new Integer(0));
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("projectActionForm", projectValueActionForm);
        ProjectValue projectValue = new ProjectValue();
        projectValue.setTaskValue(new TaskValue());
        projectValue.getTaskValue().setNodeValue(new NodeValue());
        session.setAttribute("projectValue", projectValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ProjectPK projectPK = new ProjectPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        ProjectValue projectValue = cs.getProjectValue(key, projectPK);
        session.setAttribute("projectValue", projectValue);
        ProjectValueActionForm projectValueActionForm = new ProjectValueActionForm();
        projectValueActionForm.setName(projectValue.getTaskValue().getNodeValue().getName());
        projectValueActionForm.setDescription(projectValue.getTaskValue().getNodeValue().getDescription());
        projectValueActionForm.setPriority(projectValue.getTaskValue().getPriority());
        projectValueActionForm.setSponsorId(projectValue.getSponsorPersonValue().getPersonId());
        projectValueActionForm.setPrimaryContactId(projectValue.getPrimaryContactPersonValue().getPersonId());
        projectValueActionForm.setOwnerId(projectValue.getTaskValue().getOwnerContactableValue().getContactableId());
        projectValueActionForm.setTaskTypeId(projectValue.getTaskValue().getTaskStateMachineValue().getTaskStateMachineId());
        projectValueActionForm.setPercentageComplete(projectValue.getTaskValue().getPercentageComplete());
        projectValueActionForm.setDurationTotalMins(projectValue.getTaskValue().getExpectedDuration_min());
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("projectActionForm", projectValueActionForm);
        session.setAttribute("personPairs", NodeUtil.getAllPersonPairs(key, cs));
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        Collection taskTypeIntegerStrings = new ArrayList();
        Iterator i = cs.getAllTaskStateMachineValues(key).iterator();
        while(i.hasNext()) {
            TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)i.next();
            taskTypeIntegerStrings.add(new IntegerString(taskStateMachineValue.getTaskStateMachineId(), taskStateMachineValue.toString()));
        }
        session.setAttribute("taskTypePairs", taskTypeIntegerStrings);
        session.setAttribute("allContactablePairs", NodeUtil.getAllContactablePairs(key, cs));
        Collection interestedContactableIntegerStrings = new ArrayList();
        i = cs.getTaskInterestedContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            interestedContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("interestedContactablePairs", interestedContactableIntegerStrings);
        Collection responsibleContactableIntegerStrings = new ArrayList();
        i = cs.getTaskResponsibleContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            responsibleContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("responsibleContactablePairs", responsibleContactableIntegerStrings);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            ProjectValueActionForm projectValueActionForm = (ProjectValueActionForm)form;
            ProjectValue projectValue = (ProjectValue)session.getAttribute("projectValue");
            projectValue.getTaskValue().getNodeValue().setName(projectValueActionForm.getName());
            projectValue.getTaskValue().getNodeValue().setDescription(projectValueActionForm.getDescription());
            projectValue.getTaskValue().setPriority(projectValueActionForm.getPriority());
            PersonValue sponsorPersonValue = new PersonValue();
            sponsorPersonValue.setPrimaryKey(new PersonPK(projectValueActionForm.getSponsorId()));
            projectValue.setSponsorPersonValue(sponsorPersonValue);
            PersonValue primaryContactPersonValue = new PersonValue();
            primaryContactPersonValue.setPrimaryKey(new PersonPK(projectValueActionForm.getPrimaryContactId()));
            projectValue.setPrimaryContactPersonValue(primaryContactPersonValue);
            ContactableValue ownerContactableValue = new ContactableValue();
            ownerContactableValue.setPrimaryKey(new ContactablePK(projectValueActionForm.getOwnerId()));
            projectValue.getTaskValue().setOwnerContactableValue(ownerContactableValue);
            TaskStateMachineValue taskStateMachineValue = new TaskStateMachineValue();
            taskStateMachineValue.setPrimaryKey(new TaskStateMachinePK(projectValueActionForm.getTaskTypeId()));
            projectValue.getTaskValue().setTaskStateMachineValue(taskStateMachineValue);
            projectValue.getTaskValue().setPercentageComplete(projectValueActionForm.getPercentageComplete());
            projectValue.getTaskValue().setExpectedDuration_min(projectValueActionForm.getDurationTotalMins());
            ProjectPK projectPK;
            if (projectValue.projectIdHasBeenSet()) {
                projectPK = projectValue.getPrimaryKey();
                cs.projectEdit(key, projectPK, projectValue, projectValueActionForm.getResponsibleContactablePKs(), projectValueActionForm.getInterestedContactablePKs());
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                projectPK = cs.projectCreate(key, parentNodePK, projectValue);
                if (((ProjectValueActionForm)form).getIsInterested().equals(Boolean.TRUE))
                    cs.taskEnableInterestedContactableStateValue(key, new TaskPK(projectPK.getProjectId()));
            }
            nodePK = new NodePK(projectPK.getProjectId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
